package com.link2room.quintesson.facade.impl;

import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.DateUtil;
import com.link2room.quintesson.dto.result.manager.TaobaoReservationUpdateDto;
import com.link2room.quintesson.entity.TaobaoInvoice;
import com.link2room.quintesson.facade.ITaobaoReservationUpdateFacadeService;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by sss on 2017/5/3.
 */
@Component("taobaoReservationUpdateFacadeService")
public class TaobaoReservationUpdateFacadeServiceImpl implements ITaobaoReservationUpdateFacadeService {
    @Resource
    private ICommonService commonService;

    @Override
    public String resolveTaobaoReservationUpdateRQ(Element rootElement)throws Exception{
        //集团代码
        String lhotelGroupCode="";
        //酒店代码
        String lhotelCode="";

        String taoBaoOrderId="";

        String l2rNo="";

        Integer operateType=null;

        String action="";

        String status="";

        String displayText="";

        String extensions="";

        if(rootElement.element("HotelCode")!=null){
            lhotelCode=rootElement.element("HotelCode").getStringValue();
            String[] hotelOutCode=rootElement.element("HotelCode").getStringValue().split("_");
            lhotelGroupCode=hotelOutCode[0];
        }

        if(rootElement.element("TaoBaoOrderId")!=null){
            taoBaoOrderId=rootElement.element("TaoBaoOrderId").getStringValue();
        }

        if(rootElement.element("OrderId")!=null){
            l2rNo=rootElement.element("OrderId").getStringValue();
        }

        if(rootElement.element("OperateType")!=null){
            operateType=Integer.getInteger(rootElement.element("OperateType").getStringValue());
        }

        if(rootElement.element("Action")!=null){
            action=rootElement.element("Action").getStringValue();
        }

        if(rootElement.element("Status")!=null){
            status=rootElement.element("Status").getStringValue();
        }

        if(rootElement.element("DisplayText")!=null){
            displayText=rootElement.element("DisplayText").getStringValue();
        }

        if(rootElement.element("Extensions")!=null){
            extensions=rootElement.element("Extensions").getStringValue();
        }

        if(1==operateType){
            TaobaoReservationUpdateDto taobaoReservationUpdateDto=new TaobaoReservationUpdateDto(lhotelGroupCode,
                    lhotelCode,
                    taoBaoOrderId,
                    l2rNo,
                    Integer.parseInt(action),
                    Integer.parseInt(status),
                    displayText,
                    extensions);
            this.excuteTaobaoInvoice(taobaoReservationUpdateDto,rootElement.element("Invoice"));
        }
        return l2rNo;
    }


    private void excuteTaobaoInvoice(TaobaoReservationUpdateDto taobaoReservationUpdateDto,Element invoiceElement) throws Exception {
        Integer invoiceType=null;

        String invoiceTitle=null;

        String taxpayerId=null;

        Integer postType=null;

        Integer earlyPrepare=null;

        String regAddress=null;

        String receiverName=null;

        String receiverMobile=null;

        String receiverAddress=null;

        Integer provider=null;

        Integer payer=null;

        Date wantIme=null;

        String companyTel=null;

        String bankName=null;

        String bankAccount=null;

        String comment=null;

        if(invoiceElement.element("InvoiceType")!=null){
            invoiceType=Integer.parseInt(invoiceElement.element("InvoiceType").getStringValue());
        }

        if(invoiceElement.element("InvoiceTitle")!=null){
            invoiceTitle=invoiceElement.element("InvoiceTitle").getStringValue();
        }

        if(invoiceElement.element("TaxpayerID")!=null){
            taxpayerId=invoiceElement.element("TaxpayerID").getStringValue();
        }

        if(invoiceElement.element("PostType")!=null){
            postType=Integer.parseInt(invoiceElement.element("PostType").getStringValue());
        }

        if(invoiceElement.element("EarlyPrepare")!=null){
            earlyPrepare=Integer.parseInt(invoiceElement.element("EarlyPrepare").getStringValue());
        }

        if(invoiceElement.element("TaxpayerID")!=null){
            regAddress=invoiceElement.element("TaxpayerID").getStringValue();
        }

        if(invoiceElement.element("ReceiverName")!=null){
            receiverName=invoiceElement.element("ReceiverName").getStringValue();
        }

        if(invoiceElement.element("ReceiverMobile")!=null){
            receiverMobile=invoiceElement.element("ReceiverMobile").getStringValue();
        }

        if(invoiceElement.element("ReceiverAddress")!=null){
            receiverAddress=invoiceElement.element("ReceiverAddress").getStringValue();
        }

        if(invoiceElement.element("Provider")!=null){
            provider=Integer.parseInt(invoiceElement.element("Provider").getStringValue());
        }

        if(invoiceElement.element("Payer")!=null){
            payer=Integer.parseInt(invoiceElement.element("Payer").getStringValue());
        }

        if(invoiceElement.element("WantTime")!=null){
            wantIme=DateUtil.parseDateNewFormat(invoiceElement.element("WantTime").getStringValue());
        }

        if(invoiceElement.element("Comment")!=null){
            comment=invoiceElement.element("Comment").getStringValue();
        }

        TaobaoInvoice queryTaobaoInvoice=new TaobaoInvoice();
        queryTaobaoInvoice.setLhotelGroupCode(taobaoReservationUpdateDto.getLhotelGroupCode());
        queryTaobaoInvoice.setLhotelCode(taobaoReservationUpdateDto.getLhotelCode());
        queryTaobaoInvoice.setTerminalNo(taobaoReservationUpdateDto.getTerminalNo());
        queryTaobaoInvoice.setL2RNo(taobaoReservationUpdateDto.getL2RNo());
        TaobaoInvoice taobaoInvoice=commonService.findOneEQ(TaobaoInvoice.class,queryTaobaoInvoice);
        if(taobaoInvoice==null){
            taobaoInvoice=new TaobaoInvoice();
        }
        ClassConverUtil.copyProperties(taobaoReservationUpdateDto,taobaoInvoice);
        taobaoInvoice.setInvoiceType(invoiceType);
        taobaoInvoice.setInvoiceTitle(invoiceTitle);
        taobaoInvoice.setTaxpayerId(taxpayerId);
        taobaoInvoice.setPostType(postType);
        taobaoInvoice.setEarlyPrepare(earlyPrepare);
        taobaoInvoice.setRegAddress(regAddress);
        taobaoInvoice.setReceiverName(receiverName);
        taobaoInvoice.setReceiverMobile(receiverMobile);
        taobaoInvoice.setReceiverAddress(receiverAddress);
        taobaoInvoice.setProvider(provider);
        taobaoInvoice.setPayer(payer);
        taobaoInvoice.setWantIme(wantIme);
        taobaoInvoice.setCompanyTel(companyTel);
        taobaoInvoice.setBankName(bankName);
        taobaoInvoice.setBankAccount(bankAccount);
        taobaoInvoice.setComment(comment);
        commonService.saveOrUpdate(taobaoInvoice);
    }
}
